#include "DXUT.h"
#include "Biomemap.h"

void Biomemap::init(int seed, int x, int z) {
	pGen1.SetSeed(seed + 10);
	pGen2.SetSeed(seed + 11);

	TemperatureMapBuilder.SetSourceModule (pGen1);
	TemperatureMapBuilder.SetDestNoiseMap (TemperatureMap);
	TemperatureMapBuilder.SetDestSize (257, 257);
	TemperatureMapBuilder.SetBounds ((x / 1024.0), ((x+256) / 1024.0), (z / 1024.0), ((z+256) / 1024.0));
	TemperatureMapBuilder.Build ();

	HumidityMapBuilder.SetSourceModule (pGen2);
	HumidityMapBuilder.SetDestNoiseMap (HumidityMap);
	HumidityMapBuilder.SetDestSize (257, 257);
	HumidityMapBuilder.SetBounds ((x / 512.0), ((x+256) / 512.0), (z / 512.0), ((z+256) / 512.0));
	HumidityMapBuilder.Build ();
}

Biomemap::Biomemap(int seed, int x, int z) {
	_x = x;
	_z = z;
	init(seed, x, z);
}

float Biomemap::getHumidity(int x, int z) {
	return HumidityMap.GetValue(x - _x, z - _z);
}

float Biomemap::getTemperatur(int x, int z) {
	return TemperatureMap.GetValue(x - _x, z - _z);
}

BYTE  Biomemap::getBiome(int x, int z) {
	x -= _x;
	z -= _z;
	float temperature	= TemperatureMap.GetValue(x, z);
	float humidity		= HumidityMap.GetValue(x, z);
	if (( temperature - humidity ) > 1.15f)
		return DESERT;
	if (( temperature + humidity ) > 1.15f)
		return JUNGLE;
	/*
	if ( temperature > 0.7 ) {
		if ( humidity > 0.045)
			return JUNGLE;
		else if ( humidity < -0.045)
			return DESERT;
		else
			return PLAINS;
	} else */if ( temperature < -0.8 )
		return TAIGA;
	else if (( humidity > 0.1 ) && ( humidity <= 0.7 ))
		return FORREST;
	else if ( humidity > 0.7 )
		return SWAMP;
	else if ( temperature < 0.0 )
		return MOUNTAINS;
	return PLAINS;
}
